package cn.tedu.milktea.admin.controller;

import cn.tedu.milktea.admin.mapper.CategoryMapper;
import cn.tedu.milktea.admin.mapper.ProductMapper;
import cn.tedu.milktea.admin.service.ICategoryService;
import cn.tedu.milktea.common.web.JsonResult;
import cn.tedu.milktea.pojo.entity.Category;
import cn.tedu.milktea.pojo.vo.CategorySimpleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "类别管理模块")
@Slf4j
@RestController
@RequestMapping(value="/category",produces = "application/json; charset=utf-8")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;
    @Autowired(required = false)
    CategoryMapper mapper;
    @Autowired(required = false)
    ProductMapper productMapper;

    @ApiOperation("获取类别统计信息")
    @GetMapping("/statistics")
    public JsonResult<List<CategorySimpleVO>> statistics(){
        List<CategorySimpleVO> list = categoryService.getCategoryStatistics();
        return JsonResult.ok("查询完成",list);
    }

    @RequestMapping("/select")
    public List<Category> select(){
        return mapper.select();
    }
    @RequestMapping("/delete")
    public void delete(int id ){
        System.out.println("删除分类的id = " + id);
        mapper.deleteById(id);
        //删除和该分类相关的商品
        productMapper.deleteByCid(id);

    }

    @RequestMapping("/insert")
    public void insert(Category category){
        mapper.insert(category);
    }
}

